/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.gemom.auction;

import com.gemom.*;
/**
 *
 * @author Yair Diaz
 */
import java.util.*;

public class searchTree{
	
	private int numbernodesExplored=0;
        private Vector<Item> fixedItemVector;
	BidNode sroot;	
	int M = 1;
	int B=0;
	int A=3;
	int []stopMask;
	int []prevsm;
	Bid btroot;
	private double total1=0;//will hold the max revenue path value
	private Vector<Bid> provpath= new Vector<Bid>();
	private Vector<Bid> currpath=provpath;//will hold the winner path
					   
    public searchTree( Vector<Bid> listBids ){
	

        //Decoupling itemIDs from labels
        //inside BidGenerator.java
	fixedItemVector =new Vector<Item>();
        for(int i=0;i<listBids.size();i++)
        {   for(int j=0;j<listBids.get(i).getItemsVec().size();j++)
            {  if(!fixedItemVector.contains(listBids.get(i).getItemsVec().get(j)))
                    fixedItemVector.add(listBids.get(i).getItemsVec().get(j));
            }
        }//set the itemflags for each bid: i need them for the bid tree
        for(int i=0;i<listBids.size();i++){
        listBids.get(i).setItemflags(fixedItemVector.size(), fixedItemVector);
        }
        
         //Constructing the bid tree
         BidTree bt = new BidTree(listBids);
	 this.btroot= bt.getBidTreeRoot();
        
        
        //Creating stopMask for pruning the bid tree
        stopMask = new int[fixedItemVector.size()];
	stopMask[0]=M;
	for(int i=1;i<stopMask.length;i++)stopMask[i]=A;
		
	prevsm=new int[fixedItemVector.size()];
	for(int i=0;i<prevsm.length;i++)prevsm[i]=0;
	
        //Creating root node for searchTree
        sroot = new BidNode(new Bid(88));
			
        //Recursive method: Generation of the searchTree
        searchInOrder(btroot,stopMask,0,sroot,prevsm);


	}

    //Recursive method: Generation and exploration of searchTree
    public void searchtree(Bid node, int []stopMask,BidNode bidnode,int[] prevsm){

	numbernodesExplored++;		
	
        //adding new node to searchTree
        bidnode.getbch().add(new BidNode(node));
        
        if(bidnode.getbch().size()>1)bidnode.getbch().removeElementAt(0);////\/\/\/\/\optimisation
        
	bidnode= bidnode.getbch().lastElement();   
											
	double total=0;
	provpath.clear();
	BidNode bn2=sroot.getbch().lastElement();
	Bid aBid=bn2.getBid();    

	while(!bn2.getbch().isEmpty())
        {
            provpath.add(aBid);
            total+=aBid.price;
            bn2= bn2.getbch().lastElement();
            aBid=bn2.getBid();
                     
            if(bn2.getbch().isEmpty())
               {  provpath.add(aBid);
                  total+=aBid.price;
               }
            
	}//end while




//////////////////////////////////////////////////////////////////


        Vector<Bid> listing = new Vector<Bid>();
        Vector<Bid> temp = new Vector<Bid>();
        Bid first;
         while(!provpath.isEmpty())
        {
            first =provpath.firstElement();
            provpath.removeElement(provpath.firstElement());

            for(int i=0;i<provpath.size();i++)
            {
                if(first.getBrokerIdFlag()==provpath.get(i).getBrokerIdFlag())
                    temp.add(provpath.get(i));
            }
            for(int i=0;i<temp.size();i++)provpath.remove(temp.get(i));

            for(int i=0;i<temp.size();i++)
            {
                if(first.price<temp.get(i).price) first=temp.get(i);
            }
            listing.add(first);
             temp.removeAllElements();
         }

         provpath = listing;
         total=0;
         for(int i=0;i<provpath.size();i++)
            {total+=provpath.get(i).price;}

        

//////////////////////////////////////////////////////////////////////////









        
	if(total1<total){total1=total;currpath = (Vector<Bid>)provpath.clone();}
        
	total=0;
	provpath.clear();
							
	//setting the stopMask	
	int[]smclone= stopMask.clone();
	int[]prevsmc= prevsm.clone();
								
	for(int i=0;i<node.getItemflags().length;i++)prevsmc[i]=prevsmc[i]|node.getItemflags()[i];
	int check=0;
	for(int i=0;i<prevsmc.length;i++)
            {	if(prevsmc[i]==0 && check==0){smclone[i]=M;++check;}
                else if(prevsmc[i]==0 && check!=0)smclone[i]=A;
                else if(prevsmc[i]==1)smclone[i]=B;
            }
					  
	searchInOrder(btroot,smclone,0,bidnode,prevsmc);
	
}

    public void searchInOrder(Bid node, int []stopMask,int sm, BidNode bidnode,int[] prevsm) {
		
        if(sm<stopMask.length )
        {
            if(stopMask[sm]==M)
            {
               if(node!=null)node=node.left;
               if(sm==stopMask.length-1 && node!=null )
			{ //System.out.println(node+"a");
                            searchtree(node,stopMask,bidnode,prevsm);
			}
		searchInOrder(node,stopMask,++sm,bidnode,prevsm);
            }
            else if(stopMask[sm]==B )
            {
                    if(node!=null)node=node.right;
		    if(sm==stopMask.length-1 && node!=null )
			{//System.out.println(node+"b");
                            searchtree(node,stopMask,bidnode,prevsm);
			}
                searchInOrder(node,stopMask,++sm,bidnode,prevsm);	
            }
            else if(stopMask[sm]==A)
            {
		Bid v1 = node;
		int sm1 = sm;
			
		if(node!=null)node=node.left;
	  	if(sm==stopMask.length-1 && node!=null)
                        { //System.out.println(node+"c");
                            searchtree(node,stopMask,bidnode,prevsm); 
                        }
		searchInOrder(node,stopMask,++sm,bidnode,prevsm);
			
		if(v1!=null)node=v1.right;        
		if(sm1==stopMask.length-1 && v1!=null && node!=null)
                        {//System.out.println(node+"d");
                            searchtree(node,stopMask,bidnode,prevsm);	
                        }
		searchInOrder(node,stopMask,++sm1,bidnode,prevsm);
            }
	 }//end outer if		
    }//end method

    public void printstree(BidNode bidnode)
    {
 	  for(int i=0;i<bidnode.getbch().size();i++){
 		System.out.println(bidnode.getbch().get(i).getBid());
 	 	printstree(bidnode.getbch().get(i));}		
    
    }//end method
 
    public int getNodesExplored(){return numbernodesExplored;}
    public Vector<Bid>  getWinnerPath(){return currpath;}
    public double getTotalWinnerPath(){return total1;}
    
}//end class